<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;
use think\cache\driver\File;

/**
 * vue商城
 *商品分类管理
 * @icon fa fa-user
 */
class Type extends Backend
{
    /**
     * 查看
     */
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Product.Type');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            if (!empty($params)) {
                $this->error("参数不能为空");
            }

            $params = $this->request->post('row/a');
            $res['code'] = 1;
            if (!empty($params['thumb'])) {
                if (!uploading($params['thumb'])) {
                    $this->error("上传图片到后台失败");
                }
            }
            $result = $this->model->validate('common/Product/Type')->save($params);
            if (!$result) {
                $this->error($this->model->getError());
            }
            $this->success();
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);
        if (!$row) {
            $this->error(__('当前商品单位不存在'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');
            $params['id'] = $ids;
            if (empty($params)) {
                $this->error("参数不能为空");
            }
            $result = $this->model->validate('common/Product/Type')->isUpdate()->save($params);
            if ($result === false) {
                // 判断图片是否有上传
                if (!empty($params['thumb']) && $params['thumb'] != $row['thumb']) {
                    is_file(ltrim($params['thumb'], '/')) && unlink(ltrim($params['thumb'], '/'));
                }
                if (delImg($params['thumb'])) {
                    $this->error("删除后台图片失败");
                }
                $this->error($this->model->getError());
            }
            // 判断图片是否有上传
            if (!empty($params['thumb']) && $params['thumb'] != $row['thumb']) {

                if (!uploading($params['thumb'])) {
                    $this->error("上传图片到后台失败");
                }

                is_file(ltrim($row['thumb'], '/')) && unlink(ltrim($row['thumb'], '/'));
                delImg($row['thumb']);
            }
            $this->success();
        }

        $this->assign('row', $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = null)
    {
        $List = $this->model->all($ids);
        if (empty($List)) {
            $this->error('当前商品分类不存在');
        }
        $result = $this->model->destroy($ids);
        if (!$result) {
            $this->error($this->model->getError());
        }
        foreach ($List as $item) {
            is_file(ltrim($item['thumb'], '/')) && unlink(ltrim($item['thumb'], '/'));
            delImg($item['thumb']);
        }
        $this->success();
    }
}
